package com.woniu51.user.service.impl;

import com.woniu.common.constant.UserConstants;
import com.woniu.common.core.redis.RedisCache;
import com.woniu.common.enums.ExceptionCode;
import com.woniu.common.exception.CustomException;
import com.woniu.common.utils.sign.Md5Utils;
import com.woniu51.dto.RegisterDto;
import com.woniu51.entity.User;
import com.woniu51.user.mapper.UserMapper;
import com.woniu51.user.service.UserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 杨光
 * @date 2022年03月29日 11:10
 */
@Service
public class UserSeviceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Value("${md5salt}")
    private String md5salt;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Override
    public User queryUserByUserAccountAndPassword(String userAccount, String userPassword) {
        User user = new User();
        user.setUserAccount(userAccount);
        //先把用户传入的明文密码加密，然后使用加密的密码去数据库查找
        String encodedUserPassword = Md5Utils.hash(userPassword+md5salt);
        user.setUserPassword(encodedUserPassword);

        //根据用户account和password查询
        User result = userMapper.selectOne(user);
        return result;
    }

    @Override
    public Integer checkCellphoneExist(String cellPhone) {

        User user = new User();
// 因为用户是使用手机号码登录，所以这里查询的时候，把手机号码放入userAccount字段查询
        user.setUserAccount(cellPhone);

        User result = userMapper.selectOne(user);

        return result==null?0:1;

    }

    @Override
    public Integer queryVerifyCode(String cellPhone) {

        //生成短信验证码
        String code = (System.currentTimeMillis() + "").substring(9);
        //把验证码放入redis，设置1分钟有效
        redisCache.setCacheObject(UserConstants.REDIS_REGISTER_VERIFY_CODE_PREFIX+cellPhone,code,1, TimeUnit.MINUTES);
        //发消息给rabbitmq，消息微服务收到后会发短信给用户手机
        Map<String,String> msg = new HashMap();
        msg.put(UserConstants.REGISTER_MSG_KEY_CELLPHONE,cellPhone);
        msg.put(UserConstants.REGISTER_MSG_KEY_CODE,code);

        rabbitTemplate.convertAndSend(UserConstants.MQ_REGISTER_EXCHANGE,UserConstants.MQ_REGISTER_ROUTING_KEY,msg);

        return 1;
    }

    @Override
    public Integer register(RegisterDto registerDto) {
        //1.检查验证码是否正确
        String code = redisCache.getCacheObject(UserConstants.REDIS_REGISTER_VERIFY_CODE_PREFIX + registerDto.getCellPhone());
        if(code==null || !code.equals(registerDto.getVerifyCode())){
            throw new CustomException(ExceptionCode.INVALID_KAPTCHA);
        }

        //2.验证是否已经被注册
        User user1 = new User();
        user1.setUserAccount(registerDto.getUserAccount());
        User result = userMapper.selectOne(user1);
        if(result!=null){
            throw new CustomException(ExceptionCode.USER_ACCOUNT_IN_USE);
        }
        //3.加密密码
        String encodedUserPassword = Md5Utils.hash(registerDto.getUserPassword()+md5salt);
        //4.添加用户
        User user = new User();
        user.setUserAccount(registerDto.getUserAccount());
        user.setUserPhone(registerDto.getCellPhone());
        user.setUserPassword(encodedUserPassword);
        user.setUserType(2);
        user.setUserStatus(1);

        return userMapper.insertSelective(user);

    }

    public static void main(String[] args) {
        String hash = Md5Utils.hash("111" + "JHSLSHS887slddd");
        System.out.println(hash);

        String substring = (System.currentTimeMillis() + "").substring(9);
        System.out.println(substring);
    }
}
